GPIO : General purpose I/O

更新时间:
2024-05-13
下载文档

GPIO : General purpose I/O

This module is a General-Purpose I/O operation module that supports GPIO read, write and interrupt triggering.

User can use the following code to import the Gpio module.

var Gpio = require('gpio');

Support

The following shows Gpio module APIs available for each permissions.

 User ModePrivilege Mode
Gpio 
Gpio.open 
gpio.value 
gpio.close 
gpio.wait 
gpio.async 
gpio.getMode 
gpio.setMode 
gpio.getBrightness 
gpio.setBrightness 

Gpio Class

new Gpio(number[, flags])

  • number {Integer} GPIO number.
  • flags {Integer} GPIO open flags. default: Gpio.DIR_IN.
  • Returns: {Object} Returns GPIO object.

flags can be the following value (Bits or):

Direction

  • Gpio.DIR_IN Input mode. Otherwise output mode.

Output Initial state

  • Gpio.INIT_HIGH Initial high. Otherwise low.

Outputs mode

  • Gpio.OPEN_DRAIN Open drain.
  • Gpio.OPEN_SOURCE Open source.

Pull mode

  • Gpio.PULL_UP Pull up.
  • Gpio.PULL_DOWN Pull down.

Interrupt mode

  • Gpio.TRIG_FALL Fall edge or low level.
  • Gpio.TRIG_RISE Rise edge or high level.
  • Gpio.TRIG_LEVEL Level interrupt or edge trigger.

Open a GPIO file with the specified number and modes.

Example

// Open gpio for output.
var gpio = new Gpio(10, Gpio.INIT_HIGH);

// Open gpio for output initiaze low.
var gpio = new Gpio(10, 0);

// Open gpio for input with pull up.
var gpio = new Gpio(10, Gpio.DIR_IN | Gpio.PULL_UP);

// Open gpio for input with fall edge interrupt.
var gpio = new Gpio(10, Gpio.DIR_IN | Gpio.TRIG_FALL);

Gpio.open(number[, flags])

  • number {Integer} GPIO number.
  • flags {Integer} GPIO open flags. default: Gpio.DIR_IN.
  • Returns: {Object} Returns GPIO object.

Open a GPIO file with the specified number and modes.

Same as new Gpio(), but does not throw an exception, returning undefined means opening failed.

Example

// Open gpio for input.
var gpio = Gpio.open(10);

Gpio Object

gpio.value

  • {Integer}

Read gpio.value can get the current level status of GPIO. If this GPIO is output mode, write 1 to output high level and 0 to output low level.

Example

while (true) {
  // Reverse every second.
  gpio.value = !gpio.value;
  sys.sleep(1000);
}

gpio.close()

Close this GPIO and reclaiming file descriptors. When in synchronous mode if user forgets to call this function, the file descriptor is automatically reclaimed when the object is destroyed.

gpio.wait([timeout])

  • timeout {Integer} Wait timeout in milliseconds. default: undefined means wait forever.
  • Returns: {Boolean} Is there an interruption.

Waiting for GPIO interrupt. GPIO must enable interrupt. gpio defaults use asynchronous mode, users must first call gpio.async(false) to convert to synchronous mode before using this method.

Example

  • Synchronize
// Open gpio for input with fall edge interrupt.
var gpio = Gpio.open(10, Gpio.DIR_IN | Gpio.TRIG_FALL);
gpio.async(false); // convert to synchronous mode

while (true) {
  gpio.wait(); // Block until interrupt arrives.
  console.log('Interrupt, value:', gpio.value);
}

gpio.async([enable])

  • enable {Boolean} Whether to enable asynchronous mode. default: true.

Wait interrupt using asynchronous mode. After enabling this mode, the gpio.wait() method will be deleted.

Example

gpio.async(); // asynchronous mode
gpio.async(false); // synchronous mode

gpio.getMode()

  • Returns: {Integer} GPIO mode.

Get current mode of GPIO.

gpio.setMode(flags)

  • flags {Integer} GPIO open flags.

Set current mode of GPIO.

Example

var flags = gpio.getMode();
if (!(flags & Gpio.DIR_IN)) {
  gpio.setMode(flags | Gpio.DIR_IN);
}

gpio.getBrightness()

  • Returns: {Integer} Brightness 0 ~ 100.

Some processor GPIOs have the ability to control voltage and current, and can control the brightness or color of the LED when driving the LED. This API can be used to get the brightness of the LED.

gpio.setBrightness(level)

  • level {Integer} Brightness 0 ~ 100.
  • Returns: {Boolean} Whether succeed.

This API can be used to set the brightness of the LED.

Gpio Events

The Gpio class inherits from the EventEmitter class. The following events are thrown in some specific situations.

interrupt

Interrupt received on current gpio pin. Only gpio with the interrupt trigger flag set can receive the this event.

Example

gpio.on('interrupt', function() {
  console.log('Interrupt! value:', gpio.value);
});
文档内容是否对您有所帮助?
有帮助
没帮助